package com.cy.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cy.condition.BackUserCondition;
import com.cy.dao.BackRoleDao;
import com.cy.dao.BackUserDao;
import com.cy.dao.BackUserRoleDao;
import com.cy.entity.BackPurview;
import com.cy.entity.BackRole;
import com.cy.entity.BackUser;
import com.cy.entity.BackUserRole;
import com.cy.entity.NormalStatus;
import com.cy.service.BackUserService;
import com.cy.util.Configure;
import com.cy.util.Md5Util;
import com.cy.util.StringUtils;

@Service("backUserService")
public class BackUserServiceImpl implements BackUserService<BackUser> {

	private BackUserDao backUserDao;
	private BackUserRoleDao backUserRoleDao;
	private BackRoleDao backRoleDao;
	
	public BackUser saveBackUser(BackUser backUser) {
		backUser.setCreateTime(new Date());
		backUser.setStatus(NormalStatus.NORMAL);
		backUser.setPassword(Md5Util.MD5Encode(backUser.getPassword()));
		return backUserDao.saveBackUser(backUser);
	}

	public Boolean loginIndex(BackUser backUser) {
		if(StringUtils.isEmpty(backUser.getLoginName().trim()) || StringUtils.isEmpty(backUser.getPassword().trim())){
			return false;
		}
		
		if(backUser.getLoginName().equals(Configure.getString("vvmall.superAdmin.loginId"))){
			if(backUser.getPassword().equals(Configure.getString("vvmall.superAdmin.cyPassWord"))){
				return true;
			}else{
				return false;
			}
		}
		
		BackUser userDB=findBackUserByLoginName(backUser.getLoginName());
		if(userDB==null){
			return false;
		}else{
			if(userDB.getPassword().equals(Md5Util.MD5Encode(backUser.getPassword()) )){
				return true;
			}else{
				return false;
			}
		}
	}
	
	
	
	public BackUser findBackUserByLoginName(String loginName) {
		return backUserDao.findBackUserByLoginName(loginName);
	}
	
	public List<BackUser> listBackUserByCondition(int firstResult,int maxResults, BackUserCondition condition) {
		return backUserDao.listBackUserByCondition(firstResult, maxResults, condition);
	}
	
	public int countBackUserByCondition(BackUserCondition condition) {
		return backUserDao.countBackUserByCondition(condition);
	}
	
	public BackUser editBackUserDWR(BackUser backUser, Long id) {
		BackUser oldBackUser= backUserDao.getObject(BackUser.class, id);
		oldBackUser.setNickname(backUser.getNickname());
		return oldBackUser;
	}
	
	public void ralationBackUserAndRole(Long userId, String roleStr) {
		List<BackUserRole> backUserRoles=new ArrayList<BackUserRole>();
		BackUser backUser=backUserDao.getObject(BackUser.class, userId);
		backUserRoleDao.deleteBackUserRoleByUser(backUser);
		for(String roleId:roleStr.split(",")){
			BackUserRole backUserRole=new BackUserRole();
			backUserRole.setBackUser(backUser);
			backUserRole.setRole(backRoleDao.getObject(BackRole.class, Long.parseLong(roleId)));
			backUserRoles.add(backUserRole);
		}
		backUserRoleDao.saveObjects(backUserRoles);
	}
	
	public List<BackRole> listBackRoleByBackUser(BackUser backUser) {
		List<BackRole> backRoles=new ArrayList<BackRole>();
		List<BackUserRole> backUserRoles=backUserRoleDao.listBackUserRoleByBackUser(backUser);
		for(BackUserRole backUserRole:backUserRoles){
			backRoles.add(backUserRole.getRole());
		}
		return backRoles;
	}
	
	public void updateRoleRelationBackUsers(BackRole backRole,String backUserStr) {
		backUserRoleDao.deleteBackUserRoleByRole(backRole);
		if(StringUtils.isNotEmpty(backUserStr)){
			List<BackUserRole> backUserRoles=new ArrayList<BackUserRole>();
			for(String backUserId:backUserStr.split(",")){
				BackUserRole backUserRole=new BackUserRole();
				BackUser backUser=new BackUser();
				backUser.setId(Long.parseLong(backUserId));
				backUserRole.setRole(backRole);
				backUserRole.setBackUser(backUser);
				backUserRoles.add(backUserRole);
			}
			backUserRoleDao.saveObjects(backUserRoles);
		}
		
	}
	
	public BackUser findBackUserByLoginNameAndPassWord(String loginName,String password) {
		return backUserDao.findBackUserByLoginNameAndPassWord(loginName, password);
	}

	public List<BackPurview> listBackUserAllPurview(Long id) {
		return backUserDao.listBackUserAllPurview(id);
	}
	
	@Override
	public BackUser getObject(Serializable id) {
		return backUserDao.getObject(BackUser.class, id);
	}

	@Override
	public List<BackUser> getAllObjects() {
		return null;
	}

	@Override
	public void removeObject(BackUser obj) {
		
	}

	@Override
	public BackUser saveObject(BackUser obj) {
		return null;
	}

	@Override
	public BackUser updateObject(BackUser obj) {
		return null;
	}
	
	public BackUserDao getBackUserDao() {
		return backUserDao;
	}
	@Autowired
	public void setBackUserDao(BackUserDao backUserDao) {
		this.backUserDao = backUserDao;
	}

	public BackUserRoleDao getBackUserRoleDao() {
		return backUserRoleDao;
	}
	@Autowired
	public void setBackUserRoleDao(BackUserRoleDao backUserRoleDao) {
		this.backUserRoleDao = backUserRoleDao;
	}

	public BackRoleDao getBackRoleDao() {
		return backRoleDao;
	}
	@Autowired
	public void setBackRoleDao(BackRoleDao backRoleDao) {
		this.backRoleDao = backRoleDao;
	}




	



	
	
	
	
	
	

}
